package com.iris.live.services.data.specifications;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;

import java.io.Serializable;
import java.util.List;

/**
 * Created by xin on 2016/7/12.
 */
public class SpecificationUtils {

    public static <T> Specification<T> isKeyEqual(String key, Serializable value) {
        return (root, query, cb) -> cb.equal(root.get(key), value);
    }

    public static <T> Specification<T> andAllOrNull(List<Specification<T>> specs) {
        if (specs == null || specs.isEmpty()) {
            return null;
        }

        Specifications<T> result = Specifications.where(specs.get(0));
        for (int i = 1; i < specs.size(); i++) {
            result = result.and(specs.get(i));
        }

        return result;
    }
}
